<template>
  <div class="flexContent">
    <!-- 采退出库 -->
    <div class="searchBox">

      <el-form ref="form" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="采退编号">
                  <el-input v-clearEmoji v-model.number="sform.number" placeholder="请输入采退编号" style="width:95%;" clearable
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="供应商">
                  <el-select @click.native="getSCMList" v-model="sform.supplierId" clearable style="width:95%;"
                    popper-class="eloption" placeholder="请选择供应商" filterable>
                    <el-option v-for="item in supplierList" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="提交时间">
                  <el-date-picker v-model="date" clearable type="daterange" unlink-panels range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width:95%;" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetForm" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="toAddPage" v-has="'purchaseback_add'">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" title="序号" width="80" />
          <vxe-column field="number" title="采退编号" :min-width="elTableAdaptationWidth('number', tableData)" />
          <vxe-column field="supplierName" title="供应商名称" :min-width="elTableAdaptationWidth('supplierName', tableData)" />
          <vxe-column field="returnMoney" title="退货金额（元）" min-width="150" />
          <vxe-column field="warehouseName" title="出库仓库"
            :min-width="elTableAdaptationWidth('warehouseName', tableData)" />
          <vxe-column field="returnTime" title="提交时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.returnTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column fixed="right" title="操作" width="100" v-if="$showFn(['purchaseback_detail'])">
            <template slot-scope="scope">
              <el-button type="text" @click="toDetail(scope.row.id)" v-has="'purchaseback_detail'">详情</el-button>
            </template>
          </vxe-column>
        </vxe-table>

      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getSCMList } from "@/api/scm";
import { getBuyReturnsSelectist } from "@/api/warehouse";
export default {
  name: "PurchaseBack",
  data() {
    return {
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      sform: {
        number: "",
        maxDate: "",
        minDate: "",
        supplierId: "",
      },
      supplierList: [],
      tableData: [],
    };
  },
  created() {
    // this.getSCMList();
    this.getBuyReturnsSelectist();
  },
  methods: {
    async getSCMList() {
      const res = await getSCMList();
      if (res.code === 200) {
        this.supplierList = res.data.list;
        console.log(this.supplierList, "supplierList");
      }
    },
    async getBuyReturnsSelectist() {
      if (!this.isSearch) {
        this.sform = {
          number: "",
          maxDate: "",
          minDate: "",
          supplierId: "",
        };
      }
      const res = await getBuyReturnsSelectist({ ...this.page, ...this.sform });
      if (res.code === 200) {
        this.tableData = res.data.list;
        this.page.pageSize = res.data.pageSize;
        this.page.pageNum = res.data.pageNum;
        this.page.total = res.data.total;
        console.log(res.data, "退货单");
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      }
    },
    resetForm(formName) {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.sform = {
        number: "",
        maxDate: "",
        minDate: "",
        supplierId: "",
      };
      this.isSearch = false;
      this.date = [];
      this.getBuyReturnsSelectist();
    },
    toAddPage() {
      this.$router.push({
        path: "purchaseBackAdd",
      });
    },
    toDetail(id) {
      console.log(id);
      this.$router.push({
        path: "purchaseBackDetail",
        query: {
          id: id,
        },
      });
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);

      this.getBuyReturnsSelectist();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;

      this.getBuyReturnsSelectist();
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };

      this.isSearch = true;
      this.sform.maxDate = this.date?.length ? this.date[1] : "";
      this.sform.minDate = this.date?.length ? this.date[0] : "";
      this.getBuyReturnsSelectist();
    },
  },
};
</script>

<style lang="scss" scoped></style>
